package com.example.administrator.zhongyichou.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.adapter.NewsAdapter;
import com.example.administrator.zhongyichou.base.BaseFragment;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.net.protocol.NewsProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 第五版新加
 * Created by lichenyu on 2017/11/20.
 * 描述：消息
 */

public class NewsFragment extends BaseFragment<NewsProtocol> {
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.rl_empty)
    RelativeLayout rlEmpty;
    private int page = 1;
    private int pageSize = 10;
    private NewsProtocol protocol;
    private NewsAdapter adapter;

    @Override
    protected void responseData(NewsProtocol mGsonBaseProtocal) {
        refresh.finishRefresh();
        protocol = mGsonBaseProtocal;
        adapter = new NewsAdapter(getContext(), protocol.getData());
        listview.setAdapter(adapter);
        listview.setEmptyView(rlEmpty);
    }

    @Override
    protected HttpLoader initHttpLoader() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(NewsProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("page_size", pageSize + "");
        map.put("user_id", MyApplication.getUserID() + "");
        requestHttpLoader.setParams(NetConfig.USER_NEWS, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected View initContentView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.news_fragment, null);
        ButterKnife.bind(this, view);
        refresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (page == protocol.getTotal_page()) {
                    refresh.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showToast("已经到底啦");
                            refresh.finishLoadmore();
                        }
                    }, 1000);
                    return;
                }
                page++;
                addData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                requestData();
            }
        });
        return view;
    }


    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    private void addData() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(NewsProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("page_size", pageSize + "");
        map.put("user_id", MyApplication.getUserID() + "");
        requestHttpLoader.setParams(NetConfig.USER_NEWS, map);
        requestHttpLoader.executor(new ResultListener<NewsProtocol>() {
            @Override
            public void success(NewsProtocol mGsonBaseProtocol) {
                refresh.finishLoadmore();
                protocol.getData().addAll(mGsonBaseProtocol.getData());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }
}
